package com.hackerrank.challenges.findaword;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		in.nextLine();
		Map<String, Integer> c = new HashMap<String, Integer>();
		for(int n = 0; n < N; n++) {
			String s[] = in.nextLine().split("\\W+");
			for(String t : s) {
				Integer cv = c.get(t);
				if(cv == null)
					cv = 0;
				c.put(t, cv + 1);
			}
		}
		int T = in.nextInt();
		in.nextLine();
		for(int t = 0; t < T; t++) {
			String w = in.nextLine();
			Integer cv = c.get(w);
			if(cv == null)
				cv = 0;
			System.out.println(cv);
		}
	}
}
